/**
 * ihome passport 相关的JS
 */

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}


function delCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

/**
 * 退出后返回当前页面
 */
function logout() {
    unsetIhomeCookie();
	var re_url = window.location.host;
	var global_current_location = 'http://'+re_url;
	var ru = global_current_location;
   	var time = new Date().getTime();


	var str = '/' + 'key' + '/[^\\/]+(?=\.html|\/)';
	re = /i/;
	re.compile(str);
	ru = ru.replace(re, '');
	
   	location.href = "http://passport.sohu.com/sso/logout_js.jsp?s=" + time + "&ru=" + ru;
	return false;
}

function logoutHomepage() {
    unsetIhomeCookie();

    var ru = "http://xiaoliangyang.shangcheng.sohu.com";
    var time = new Date().getTime();
    window.location = "http://passport.sohu.com/sso/logout_js.jsp?s=" + time + "&ru=" + ru;
}

/**
 * 删除爱家会员Cookie
 */
function unsetIhomeCookie() {
    delCookie('passport_ihome', '/', 'shangcheng.sohu.com');
}

/*
 * JS 以POST方法访问某URL
 */
function tuan_post(URL, PARAMS) {        
	var temp = document.createElement("form");        
	temp.action = URL;        
	temp.method = "post";        
	temp.style.display = "none";        
	for (var x in PARAMS) {        
		var opt = document.createElement("textarea");        
		opt.name = x;        
		opt.value = PARAMS[x];        
		temp.appendChild(opt);        
	}        
	
	document.body.appendChild(temp);        
	temp.submit();        
	return temp;        
} 


function PassportLayer () 
{
	/*
	 * 检查当前用户角色:
	 *	若匿名则弹出登录框;
	 *	若买家登录则直接回跳地址;
	 *	若商家登录则弹出提示;
	 */
	this.check = function (url, auto, merchant_tip)
	{
		var sUrl  = (typeof(url)  == 'undefined' ? '' : url);
		var bAuto = (typeof(auto) == 'undefined' ? true : auto);
		var sTip  = (typeof(merchant) == 'undefined' ? '您现在是商家，不能使用该功能!' : merchant_tip);

		if (! bAuto )
		{
			this.show_login();	
			return false;
		}

		if (current_merchant_id - 0 > 0)
		{
			alert(sTip);
			return false;
		}

		if (current_user_id - 0 > 0)
		{
			if (sUrl)
				window.location.assign(sUrl);
			return false;
		}

		this.show_login();
		return false;
	}

	this.show_login = function () 
	{
		var mask_l  = $('#layer-mask');
		var login_l = $('#layer-login');

		var viewWidth	= document.documentElement.clientWidth;
		var viewHeight	= document.documentElement.clientHeight;
		var scrollY		= document.documentElement.scrollTop || document.body.scrollTop;
		var documentHeight = Math.max(document.documentElement.scrollHeight || document.body.scrollHeight, document.documentElement.clientHeight);
	
		mask_l.css('width',	 viewWidth + 'px');
		mask_l.css('height', documentHeight + 'px');

		var ll = document.getElementById("layer-login");
		login_l.css('position', 'absolute');
		login_l.css('left', (viewWidth  - ll.offsetWidth)/2 + 'px');
		login_l.css('top',	(viewHeight - ll.offsetHeight)/2 + scrollY + 'px');

		// 载入登录窗口
		if (! login_l.attr('loaded'))
		{
			this.handle_window_scroll ();
			$.get('/passport/window/login', {}, function (o) {
					var login_l = $('#layer-login');
					var mask_l  = $('#layer-mask');
					if (o.status == 0)
					{
						var viewWidth	= document.documentElement.clientWidth;
						var viewHeight	= document.documentElement.clientHeight;
						var scrollY		= document.documentElement.scrollTop || document.body.scrollTop;
						login_l.html(o.content);
						login_l.attr('loaded', 1);
						mask_l.show();
						login_l.show();
						var ll      = document.getElementById("layer-login");
						ll.style.position = 'absolute';
						ll.style.left	= (viewWidth - ll.offsetWidth)/2 + 'px';
						ll.style.top	= (viewHeight - ll.offsetHeight)/2 + scrollY + 'px';
					}
					else
						passportMgr.close_login();

				}, 'json');
		}
		else
		{
			mask_l.show();
			login_l.show();

			var viewWidth	= document.documentElement.clientWidth;
			var viewHeight	= document.documentElement.clientHeight;
			var documentHeight = Math.max(document.documentElement.scrollHeight || document.body.scrollHeight, document.documentElement.clientHeight);
			var scrollY		= document.documentElement.scrollTop || document.body.scrollTop;

			ll.style.position = 'absolute';
			ll.style.left	= (viewWidth - ll.offsetWidth)/2 + 'px';
			ll.style.top	= (viewHeight - ll.offsetHeight)/2 + scrollY + 'px';
		}

		mask_l.show();
		login_l.show();
	},

	this.show_content = function (content_layer_id) 
	{
		var mask_l  = $('#layer-mask');
		var login_l = $('#layer-login');

		var viewWidth	= document.documentElement.clientWidth;
		var viewHeight	= document.documentElement.clientHeight;
		var scrollY		= document.documentElement.scrollTop || document.body.scrollTop;
		var documentHeight = Math.max(document.documentElement.scrollHeight || document.body.scrollHeight, document.documentElement.clientHeight);
	
		mask_l.css('width',	 viewWidth + 'px');
		mask_l.css('height', documentHeight + 'px');

		var ll = document.getElementById("layer-login");

		mask_l.show();
		login_l.show();
		login_l.html($('#' + content_layer_id).html());

		var viewWidth	= document.documentElement.clientWidth;
		var viewHeight	= document.documentElement.clientHeight;
		var documentHeight = Math.max(document.documentElement.scrollHeight || document.body.scrollHeight, document.documentElement.clientHeight);
		var scrollY		= document.documentElement.scrollTop || document.body.scrollTop;

		ll.style.position = 'absolute';
		ll.style.left	= (viewWidth - ll.offsetWidth)/2 + 'px';
		ll.style.top	= (viewHeight - ll.offsetHeight)/2 + scrollY + 'px';
		this.handle_window_scroll ();
	},

	this.set_content = function (content) {
		var login_l = $('#layer-login');
		login_l.html(content);

		var oCurr = login_l.get(0);
		var viewWidth	= document.documentElement.clientWidth;
		var viewHeight	= document.documentElement.clientHeight;
		var documentHeight = Math.max(document.documentElement.scrollHeight || document.body.scrollHeight, document.documentElement.clientHeight);
		var scrollY		= document.documentElement.scrollTop || document.body.scrollTop;

		oCurr.style.position = 'absolute';
		oCurr.style.left	= (viewWidth - oCurr.offsetWidth)/2 + 'px';
		oCurr.style.top		= (viewHeight - oCurr.offsetHeight)/2 + scrollY + 'px';
	},

	this.close_content = function ()
	{
		$('#layer-login').hide();
		$('#layer-mask').hide();
	},

	this.close_login = function ()
	{
		$('#layer-login').hide();
		$('#layer-mask').hide();
	},

	this.handle_window_scroll = function () 
	{
		window.onscroll = window.onresize = function()
		{
			var ll         = document.getElementById("layer-login");
			if(ll)
			{
				var viewWidth	= document.documentElement.clientWidth;
				var viewHeight	= document.documentElement.clientHeight;
				var documentHeight = Math.max(document.documentElement.scrollHeight || document.body.scrollHeight, document.documentElement.clientHeight);
				var scrollY		= document.documentElement.scrollTop || document.body.scrollTop;
		
				ll.style.position = 'absolute';
				ll.style.left	= (viewWidth - ll.offsetWidth)/2 + 'px';
				ll.style.top	= (viewHeight - ll.offsetHeight)/2 + scrollY + 'px';
			}	
		}
	}
}

